/**************************************************************
 * All rights reserved. No part of this file may be reproduced
 * in any form by print, photo print, microfilm or any other
 * means without written permission by ONBON.\n
 * (C) 2010-2011 Onbon
 *************************************************************/

/*!@file
 * @brief drv_i2c.h
 * @author
 * @version	1.0.0
 * @date	2010-05-21
 * @note	none
 * @bug 	none
 * @todo 	none
 */

#include "drv_i2c.h"

#include <errno.h>
#include <sys/ioctl.h>
#include<unistd.h>



#define DRVI2C0FILE_NAME "/dev/i2c-0"
// #define SPINAND_FILE_LOGO_NAME "/dev/mtd8"

Drv_I2C0* Drv_I2C0::p_drv_i2c0 = NULL;


Drv_I2C0::Drv_I2C0()
{


}


Drv_I2C0::~Drv_I2C0()
{


}





int Drv_I2C0::initialize()
{
    int ret ;

    ret = open(DRVI2C0FILE_NAME, O_RDWR);
    if (!ret)
    {
        return 0;
    }

    return ret;
}


/**
  * @brief I2C_write
  * data_infor
  * len:
  * 返回值:read_ret
  */
int Drv_I2C0::i2c_write(int fd,unsigned char slave_addr, unsigned char reg_addr, unsigned char *value ,unsigned int len)
{
#if 1
    unsigned char outbuf[300];
    struct i2c_rdwr_ioctl_data ioctl_data;
    struct i2c_msg msg[1];
    Ouint8 i;
    msg[0].addr = slave_addr>>1;
    msg[0].flags = 0;
    msg[0].len = len + 1;//sizeof(outbuf);
    msg[0].buf = outbuf;
    /* The first byte indicates which register we‘ll write */
    outbuf[0] = reg_addr;
    /*
    * The second byte indicates the value to write. Note that for many
    * devices, we can write multiple, sequential registers at once by
    * simply making outbuf bigger.
    */
    //outbuf[1] = value;
    for(i=0;i<len;i++){
        outbuf[i+1] = value[i];
    }
    /* Transfer the i2c packets to the kernel and verify it worked */
    ioctl_data.msgs = msg;
    ioctl_data.nmsgs = 1;
    if(ioctl(fd, I2C_RDWR, &ioctl_data) < 0)
    {
        //perror("Unable to send data");
        return 0;
    }
#endif


    return 1;
}


/**
  * @brief I2C_read
  * data_infor
  * len:
  * 返回值:read_ret
  */
int Drv_I2C0::i2c_read(int fd,unsigned char slave_addr, unsigned char reg_addr, unsigned char *value ,int len)
{

#if 1
     struct i2c_rdwr_ioctl_data ioctl_data;
     struct i2c_msg msg[2] ;
     unsigned char data[4] = {0};
     unsigned char out[100] = {0};
     int rc;
     int i;

     /* test reading and writing registers in PCA9554 */

     /* read from Configuration Register (0x03) */
     data[0] = reg_addr;
     msg[0].addr = slave_addr>>1;
     msg[0].flags = 0; /* write */
     msg[0].len = 1;
     msg[0].buf = &data[0];

     msg[1].addr = slave_addr>>1;
     msg[1].flags = I2C_M_RD;
     msg[1].len = len;
     msg[1].buf = &out[0];

     ioctl_data.nmsgs = 2;
     ioctl_data.msgs = &msg[0];

     //BX_printf("\nDrv_I2C0::i2c_read = %02x ,reg_addr = %02x ,fd = %02x\n",slave_addr ,reg_addr ,fd) ;

     rc = ioctl(fd, I2C_RDWR, &ioctl_data);
     if (rc < 0) {
//        printf("read, I2C_RDWR failed: %d.\n", errno);
        //close(fd);
        return (0);
     }
     for(i=0;i<len;i++){
         value[i] = out[i];
     }
     //close(fd);
#endif

    return 1;

}






